package com.hq.fxh.No3_TuiGuang.Activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetWeekStatisticalResultRes;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.Adaoter.RGStaticsAdapter;
import com.hq.fxh.No3_TuiGuang.Fragment.ExportDetailFragment;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class ExportDetailActivity extends BaseActivity {
    private List<Fragment> list_fragment;                                //定义要装fragment的列表
    private List<String> list_title;                                     //tab名称列表

    private ViewPager vp;
    private TabLayout tab_title;
    private ExportDetailFragment exportFragment;
    private ExportDetailFragment transFragment;
    private ExportDetailFragment cllickFragment;
    private ExportDetailFragment articleFragment;
    private RGStaticsAdapter adapter;
    private int position;
    private LinearLayout back;
    private int uid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_export_detail;
    }

    @Override
    protected void initView() {
        vp = (ViewPager) findViewById(R.id.stati_detail_vp);
        tab_title = (TabLayout) findViewById(R.id.stati_detail_tab_title);
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

        uid = SharedPreferenceUtils.getMemberId(this);

        Bundle bundle = this.getIntent().getExtras();
        position = bundle.getInt("position");

        exportFragment = new ExportDetailFragment();//曝光
        cllickFragment = new ExportDetailFragment();//点击
        transFragment = new ExportDetailFragment();//转发
        articleFragment = new ExportDetailFragment();//文章
        exportFragment.setSort(1);
        cllickFragment.setSort(2);
        transFragment.setSort(3);
        articleFragment.setSort(4);
        list_fragment = new ArrayList<>();
        list_fragment.add(exportFragment);
        list_fragment.add(cllickFragment);
        list_fragment.add(transFragment);
        list_fragment.add(articleFragment);

        list_title = new ArrayList<>();
        list_title.add("曝光");
        list_title.add("点击");
        list_title.add("转发");
        list_title.add("文章");

        //设置TabLayout的模式
        tab_title.setTabMode(TabLayout.MODE_FIXED);
        tab_title.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        //为TabLayout添加tab名称
        tab_title.addTab(tab_title.newTab().setText(list_title.get(0)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(1)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(2)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(3)));

        UserServiceImpl.getWeekStatisticalResult(uid + "", new NetworkSubscriber<GetWeekStatisticalResultRes>() {
            @Override
            public int onSuccess(GetWeekStatisticalResultRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    GetWeekStatisticalResultRes.DataBean datas = data.getData();
                    vp.setCurrentItem(position);
                    exportFragment.setData(0, datas.getLook());
                    exportFragment.setTextView("近7日文章点击数 " + datas.getLook().getTot() + "次");
                    cllickFragment.setData(1, datas.getClick());
                    cllickFragment.setTextView("近7日文章点击数 " + datas.getClick().getTot() + "次");
                    transFragment.setData(2, datas.getTrans());
                    transFragment.setTextView("近7日文章转发数 " + datas.getTrans().getTot() + "次");
                    articleFragment.setData(3, datas.getArticle());
                    articleFragment.setTextView("近7日文章数 " + datas.getArticle().getTot() + "篇");
                } else {
                    LogUtils.startToash(ExportDetailActivity.this, data.getMsg());

                }
                return 0;
            }
        });

        adapter = new RGStaticsAdapter(this.getSupportFragmentManager(), list_fragment, list_title);
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(4);
        tab_title.setupWithViewPager(vp);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(this, "企业近七日数据详情");
    }

    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(this, "企业近七日数据详情");
    }

}
